#!/usr/bin/env python

import twitter
import gtk.glade
import threading
import webbrowser

# Get widgets
widgets = gtk.glade.XML("nitwit.glade") 
window = widgets.get_widget("window")
window.set_icon_from_file("icon.png")
window.connect("destroy", lambda o:window.hide())
notebook_main = widgets.get_widget("nb_main")

# Create twitter and status objects
api = twitter.Api()
api.SetXTwitterHeaders("Nitwit", "http://code.google.com/p/xyhthyx", "0.1")

# Login screen controller class
class Login:
	
	def __init__(self):
		
		# Credentials
		self.user_name = None
		self.password = None
		# Get some widgets
		self.login_window = widgets.get_widget("vb_login")
		self.label_user_name = widgets.get_widget("label_user_name")
		self.entry_user_name = widgets.get_widget("entry_user_name")
		self.label_password = widgets.get_widget("label_password")
		self.entry_password = widgets.get_widget("entry_password")
		self.label_forgot_password = widgets.get_widget("label_forgot_password")
		self.label_click_here = widgets.get_widget("eb_click_here")
		self.check_remember_me = widgets.get_widget("cb_remember")
		self.check_auto_login = widgets.get_widget("cb_auto_login")
		self.button_signin = widgets.get_widget("b_signin")
		self.label_error = widgets.get_widget("label_error")
		# Connect signals
		self.label_click_here.connect("button-press-event", self.on_forgot_clicked)
		self.button_signin.connect("clicked", self.on_signin_clicked)
	
	def on_forgot_clicked(self, widget, event):
		
		webbrowser.open("http://twitter.com/account/resend_password")
	
	def on_signin_clicked(self, button):
		
		self.label_error.set_property("visible", False)
		self.login_window.set_sensitive(False)
		# Fetch user name and password from entry fields
		self.user_name = self.entry_user_name.get_text()
		self.password = self.entry_password.get_text()
		api.SetCredentials(self.user_name, self.password)
		
		# Begin thread
		threading.Thread(target=self.login).start()
	
	def login(self):
		
		try:
			status = api.PostUpdate("is being authenticated...")
			if status.id:
				self.login_window.set_sensitive(True)
				api.DestroyStatus(status.id)
				notebook_main.set_current_page(1)
			else:
				self.label_error.set_property("visible", True)
		except:
			self.label_error.set_property("visible", True)
		self.login_window.set_sensitive(True)

# Status icon controller class
class StatusIcon:
	
	def __init__(self):
		
		# Create status icon and connect callbacks
		self.status_icon = gtk.status_icon_new_from_file("icon.png")
		self.status_icon.set_tooltip('Nitwit')
		self.status_icon.connect('activate', self.on_left_click)
		self.status_icon.connect('popup-menu', self.on_right_click)
	
	def on_left_click(self, satus_icon):
		
		# Show/hide window
		if (window.get_property("visible")):
			window.hide()
		else:
			window.present()
	
	def on_right_click(self, status_icon, button, activate_time):
		
		# Create menu
		menu = gtk.Menu()
		# About
		menu_item = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
		menu_item.connect('activate', self.on_about_selected)
		menu.append(menu_item)
		# Preferences
		menu_image = gtk.Image()
		menu_item = gtk.ImageMenuItem('_Preferences')
		menu_image.set_from_stock(gtk.STOCK_PREFERENCES, gtk.ICON_SIZE_MENU)
		menu_item.set_image(menu_image)
		# menu_item.connect('activate', self.on_preferences_selected)
		menu.append(menu_item)
		# ----------
		menu.append(gtk.SeparatorMenuItem())
		# Quit
		menu_item = gtk.ImageMenuItem(gtk.STOCK_QUIT)
		menu_item.connect('activate', gtk.main_quit)
		menu.append(menu_item)
		# Popup the menu...
		menu.show_all()
		menu.popup(None, None, None, button, activate_time)
	
	def on_about_selected(self, menu_item):
		
		print threading.enumerate()


# Main run method
def run():
	# Launching...
	login = Login()
	main_program = StatusIcon()
	gtk.gdk.threads_init()
	#gtk.gdk.threads_enter()
	gtk.main()
	#gtk.gdk.threads_leave()
